﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Imaging;
using System.Globalization;

public partial class home_drawproject : System.Web.UI.Page
{
    int projectId = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["id_user"] != null && Session["permission"] != null &&
            ((int)Session["permission"]) >=2 && ((int)Session["permission"]) <= 3)
        {
            try
            {
                projectId = Int32.Parse(Request.QueryString["project"]);
            }
            catch
            {
                projectId = 0;
            }
            if (projectId == 0)
                return;
            string sql = "SELECT convert(varchar,start,103) as 'start',convert(varchar,[end],103) as 'end' FROM project WHERE id=" + comm.to_sql(projectId.ToString(), "number");
            DataTable dtTable = dal.SelectTable(sql);
            if (dtTable.Rows.Count != 1)
                return;

            DateTimeFormatInfo dateformat = new DateTimeFormatInfo();
            dateformat.ShortDatePattern = "dd/MM/yyyy";
            dateformat.DateSeparator = "/";
            DateTime start = new DateTime(2000, 1, 1);
            DateTime end = new DateTime(2010, 1, 1);
            if (dtTable.Rows[0]["start"].ToString() != "")
            {
                DateTime dtStart = Convert.ToDateTime(dtTable.Rows[0]["start"].ToString(), dateformat);
                start=start.AddDays(dtStart.Day-start.Day-1);
                start=start.AddMonths(dtStart.Month-start.Month);
                start=start.AddYears(dtStart.Year-start.Year);
            }
            if (dtTable.Rows[0]["end"].ToString() != "")
            {
                DateTime dtEnd = Convert.ToDateTime(dtTable.Rows[0]["end"].ToString(), dateformat);
                end=end.AddDays(dtEnd.Day);
                end=end.AddMonths(dtEnd.Month);
                end=end.AddYears(dtEnd.Year);
            }
            if (start.Year == 2000)
                return;

            Bitmap objBMP = new System.Drawing.Bitmap(600, 400);
            Graphics objGraphics = System.Drawing.Graphics.FromImage(objBMP);
            objGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            objGraphics.FillRectangle(new SolidBrush(Color.Green), 0, 0, objBMP.Width, objBMP.Height);

            // horizontal line
            objGraphics.DrawLine(new Pen(Color.White), new Point(0, 15), new Point(objBMP.Width, 15));
            int x = 1;
            while (x<600) // point and day
            {
                if(start.Day==1)
                    objGraphics.DrawLine(new Pen(Color.White), new Point(x, 2), new Point(x, 18));
                else
                    objGraphics.DrawLine(new Pen(Color.White), new Point(x, 14), new Point(x, 16));
                objGraphics.DrawString(start.Day.ToString(),new Font("Arial",8), new SolidBrush(Color.White), new PointF(x - 1, 16));
                x += 600 / 30;
                start = start.AddDays(1);
            }
            // month
            objGraphics.DrawString("Tháng "+start.Month.ToString()+"/"+start.Year.ToString(), new Font("Arial", 8,FontStyle.Bold), new SolidBrush(Color.White), new PointF(600/3, 0));

            Response.ContentType = "image/JPEG";
            objBMP.Save(Response.OutputStream, ImageFormat.Jpeg);
            objGraphics.Dispose();
            objBMP.Dispose();
        }
    }
}
